# -*- coding: utf-8 -*-
from . import getConnection


class FinishUrl(object):

    def __init__(self, url):
        self.url = url

    def save(self):
        connection = getConnection()
        try:
            with connection.cursor() as cursor:
                sql = "INSERT INTO `finish_url` (`url`) VALUE (%s)"
                cursor.execute(sql, (self.url, ))
            connection.commit()
        finally:
            connection.close()

    def existUrl(self):
        count = 0
        connection = getConnection()
        try:
            with connection.cursor() as cursor:
                sql = "SELECT count(*) as count FROM finish_url WHERE url = %s"
                cursor.execute(sql, (self.url, ))
                data = cursor.fetchone()
                count = data['count']
        finally:
            connection.close()

        return count > 0
